var sysId = "";
var nodeId = "";
var nodeName = "";
var tableName = "";
var nodeTypeNew = "";
var isTemplet = "N";
/**
 * 用于单元格编辑是重写的方法
 */
$.extend($.fn.datagrid.methods, {
    editCell: function(jq, param) {
        return jq.each(function() {
            var fields = $(this).datagrid('getColumnFields', true).concat(
                $(this).datagrid('getColumnFields'));
            for (var i = 0; i < fields.length; i++) {
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor1 = col.editor;
                if (fields[i] != param.field) {
                    var fieldOpts = $(this).datagrid('getColumnOption',
                        fields[i]);
                    if (!fieldOpts.hidden || fieldOpts.hidden == false) {
                        col.editor = null;
                    }
                }
            }
            $(this).datagrid('beginEdit', param.index);
            for (var i = 0; i < fields.length; i++) {
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor = col.editor1;
            }
        });
    },
    addEditor: function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item.field);
                e.editor = item.editor;
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param.field);
            e.editor = param.editor;
        }
    },
    removeEditor: function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item);
                e.editor = {};
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param);
            e.editor = {};
        }
    }
});
/**
 * 编辑数据
 */
var editIndex = undefined;

function endEditing() {
    if (editIndex == undefined) {
        return true;
    }
    if ($('#cbbTabColumnsDataGrid').datagrid('validateRow', editIndex)) {
        $('#cbbTabColumnsDataGrid').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        parent.parent.parent.$.messager.show({
        	//commonLang.tip.alert,'提示'
            title: commonLang.tip.alert,
            //commonLang.tip.checkData"请检查数据是否正确！"
            msg: commonLang.tip.checkData,
            timeout: 2000,
            showType: 'slide'
        });
        return false;
    }
}
/**
 * 单击行
 * 
 * @param index
 */
function onClickRow(rowIndex, field, value) {
    if (editIndex != rowIndex) {
        if (endEditing()) {
            $("#cbbTabColumnsDataGrid").datagrid('removeEditor',
                'colIsTabVisible'); // 删除【表格显示】编辑器
            $("#cbbTabColumnsDataGrid").datagrid('addEditor', { // 添加【表格显示】编辑器
                field: 'colIsTabVisible',
                editor: {
                    type: 'checkbox',
                    options: {
                        on: 'Y',
                        off: 'N'
                    }
                }
            });

            $("#cbbTabColumnsDataGrid").datagrid('removeEditor', 'colIsSearch'); // 删除【查询字段】编辑器
            $("#cbbTabColumnsDataGrid").datagrid('addEditor', { // 添加【查询字段】编辑器
                field: 'colIsSearch',
                editor: {
                    type: 'checkbox',
                    options: {
                        on: 'Y',
                        off: 'N'
                    }
                }
            });

            $("#cbbTabColumnsDataGrid").datagrid('removeEditor', 'colIsDetail'); // 删除【详细显示】编辑器
            $("#cbbTabColumnsDataGrid").datagrid('addEditor', { // 添加【详细显示】编辑器
                field: 'colIsDetail',
                editor: {
                    type: 'checkbox',
                    options: {
                        on: 'Y',
                        off: 'N'
                    }
                }
            });
            $('#cbbTabColumnsDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            editIndex = rowIndex;
        } else {
            $('#cbbTabColumnsDataGrid').datagrid('selectRow', editIndex);
        }
    }
}
/**
 * 提交改变的记录
 */
function accept() {
    if (endEditing()) {
        // 更新字段
        var updateRows = $("#cbbTabColumnsDataGrid").datagrid('getChanges',
            'updated');
        var updateData = JSON.stringify(updateRows);
        if (updateRows.length > 0) {
            $
                .ajax({
                    url: 'platform/aos/commtablemanagerController/updateCbbTabColumnsData.json',
                    data: {
                        param: updateData,
                        sysId: sysId,
                        nodeId: nodeId,
                        nodeName: nodeName,
                        tableName: tableName
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function(result) {
                        if (result.flag == "failure") {
                        	parent.parent.parent.$.messager.show({
                                title: commonLang.tip.alert,//'提示',
                                //commonLang.optip.saveFailure "保存失败！"
                                msg: commonLang.optip.saveFailure 
                            });
                        } else {
                            reloadDataGrid();
                            parent.parent.parent.$.messager.show({
                                title: commonLang.tip.alert,//'提示',
                                //commonLang.optip.saveSuccess "保存成功！"
                                msg: commonLang.optip.saveSuccess 
                            });
                        }
                    }
                });
        } else {
        	parent.parent.parent.$.messager.show({
                title: commonLang.tip.alert,//'提示',
                timeout: 2000,
                //commonLang.tip.alert2 没有可保存的记录！'
                msg: commonLang.tip.alert2
            });
        }
    }
}
/**
 * 回滚所有提交后改变的记录
 */
function reject() {
    $('#cbbTabColumnsDataGrid').datagrid('rejectChanges');
    editIndex = undefined;
}

/**
 * 刷新表格数据
 */
function reloadDataGrid() {
    var url = 'platform/aos/commtablemanagerController/getCbbTabColumnsData.json?sysId=' + sysId + '&tableName=' + tableName + '&isTemplet=' + isTemplet;
    $('#cbbTabColumnsDataGrid').datagrid("options").url = url;
    $("#cbbTabColumnsDataGrid").datagrid('load', {
        param: null
    });
}

/**
 * 转换是否
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
function fomaterYn(value, row, index) {
    if (value == 'Y') {
        return commonLang.fomaterType.ifYes;//"是"
    } else {
        return commonLang.fomaterType.ifNo;//"否"
    }
    return value;
}

/**
 * 【表格显示】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsTabVisible(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsTabVisible == "Y") {
        c = "<input type='checkbox' id='" + rowData.colName + "' checked='checked' value='Y' onclick='onCheckIsTabVisible(\"" + rowData.colName + "\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.colName + "' value='N' onclick='onCheckIsTabVisible(\"" + rowData.colName + "\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【表格显示】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsTabVisible(id, rowIndex) {
    $("#cbbTabColumnsDataGrid").datagrid('removeEditor', 'colIsTabVisible'); // 删除【表格显示】编辑器
    $("#cbbTabColumnsDataGrid").datagrid('addEditor', { // 添加【表格显示】编辑器
        field: 'colIsTabVisible',
        editor: {
            type: 'text'
        }
    });
    var checkbox = document.getElementById(id);
    var isCheck = false;
    if (checkbox.checked) {
        isCheck = true;
    }
    if (isCheck) {
        $('#cbbTabColumnsDataGrid').datagrid('selectRow', rowIndex).datagrid(
            'beginEdit', rowIndex);
        var editors = $('#cbbTabColumnsDataGrid').datagrid('getEditors',
            rowIndex);
        var sfgzEditor = editors[0];
        sfgzEditor.target.val("Y");
        $('#cbbTabColumnsDataGrid').datagrid('endEdit', rowIndex);
    } else {
        $('#cbbTabColumnsDataGrid').datagrid('selectRow', rowIndex).datagrid(
            'beginEdit', rowIndex);
        var editors = $('#cbbTabColumnsDataGrid').datagrid('getEditors',
            rowIndex);
        var sfgzEditor = editors[0];
        sfgzEditor.target.val("N");
        $('#cbbTabColumnsDataGrid').datagrid('endEdit', rowIndex);
    }
}

/**
 * 【查询字段】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsSearch(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsSearch == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "' checked='checked' value='Y' onclick='onCheckIsSearch(\"" + rowData.id + "\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "' value='N' onclick='onCheckIsSearch(\"" + rowData.id + "\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【查询字段】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsSearch(id, rowIndex) {
    $("#cbbTabColumnsDataGrid").datagrid('removeEditor', 'colIsSearch'); // 删除【查询字段】编辑器
    $("#cbbTabColumnsDataGrid").datagrid('addEditor', { // 添加【查询字段】编辑器
        field: 'colIsSearch',
        editor: {
            type: 'text'
        }
    });
    var checkbox = document.getElementById(id);
    var isCheck = false;
    if (checkbox.checked) {
        isCheck = true;
    }
    if (isCheck) {
        $('#cbbTabColumnsDataGrid').datagrid('selectRow', rowIndex).datagrid(
            'beginEdit', rowIndex);
        var editors = $('#cbbTabColumnsDataGrid').datagrid('getEditors',
            rowIndex);
        var sfgzEditor = editors[1];
        sfgzEditor.target.val("Y");
        $('#cbbTabColumnsDataGrid').datagrid('endEdit', rowIndex);
    } else {
        $('#cbbTabColumnsDataGrid').datagrid('selectRow', rowIndex).datagrid(
            'beginEdit', rowIndex);
        var editors = $('#cbbTabColumnsDataGrid').datagrid('getEditors',
            rowIndex);
        var sfgzEditor = editors[1];
        sfgzEditor.target.val("N");
        $('#cbbTabColumnsDataGrid').datagrid('endEdit', rowIndex);
    }
}

/**
 * 【详细显示】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsDetail(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsDetail == "Y") {
        c = "<input type='checkbox' id='" + rowIndex + "' checked='checked' value='Y' onclick='onCheckIsDetail(\"" + rowIndex + "\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowIndex + "' value='N' onclick='onCheckIsDetail(\"" + rowIndex + "\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【详细显示】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsDetail(id, rowIndex) {
    $("#cbbTabColumnsDataGrid").datagrid('removeEditor', 'colIsDetail'); // 删除【详细显示】编辑器
    $("#cbbTabColumnsDataGrid").datagrid('addEditor', { // 添加【详细显示】编辑器
        field: 'colIsDetail',
        editor: {
            type: 'text'
        }
    });
    var checkbox = document.getElementById(id);
    var isCheck = false;
    if (checkbox.checked) {
        isCheck = true;
    }
    if (isCheck) {
        $('#cbbTabColumnsDataGrid').datagrid('selectRow', rowIndex).datagrid(
            'beginEdit', rowIndex);
        var editors = $('#cbbTabColumnsDataGrid').datagrid('getEditors',
            rowIndex);
        var sfgzEditor = editors[2];
        sfgzEditor.target.val("Y");
        $('#cbbTabColumnsDataGrid').datagrid('endEdit', rowIndex);
    } else {
        $('#cbbTabColumnsDataGrid').datagrid('selectRow', rowIndex).datagrid(
            'beginEdit', rowIndex);
        var editors = $('#cbbTabColumnsDataGrid').datagrid('getEditors',
            rowIndex);
        var sfgzEditor = editors[2];
        sfgzEditor.target.val("N");
        $('#cbbTabColumnsDataGrid').datagrid('endEdit', rowIndex);
    }
}

function selectAllCol() {
    if (isTemplet == 'Y') { // 显示基本字段
        isTemplet = 'N';
        document.getElementById("selectAllCol").style.display = '';
        document.getElementById("selectCol").style.display = 'none';

    } else { // 显示所有字段
        isTemplet = 'Y';
        document.getElementById("selectAllCol").style.display = 'none';
        document.getElementById("selectCol").style.display = '';

    }
    var url = 'platform/aos/commtablemanagerController/getCbbTabColumnsData.json?sysId=' + sysId + '&tableName=' + tableName + '&isTemplet=' + isTemplet;
    $('#cbbTabColumnsDataGrid').datagrid("options").url = url;
    $("#cbbTabColumnsDataGrid").datagrid('load', {
        param: null
    });
}


function fomaterColOrder(value, row, index) {
    if (value == null || value == "null") {
        value = "";
    }
    if (row.colIsDisplay == "N" && row.colIsSys == "Y") {
        return "<font color='red'>" + value + "</font>";
    } else {
        return value;
    }
}

function fomaterColLabel(value, row, index) {
    if (value == null || value == "null") {
        value = "";
    }
    if (row.colIsDisplay == "N" && row.colIsSys == "Y") {
        return "<font color='red'>" + value + "</font>";
    } else {
        return value;
    }
}